package sharping.appilog;

import java.util.HashMap;

import sharping.appilog.Logic.DownLoadData;
import sharping.appilog.Logic.DownLoadData.OnLoadComplete;
import sharping.appilog.Logic.DownLoadData.OnLoadError;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;

public class UserProfileEditText extends Activity {

	public static final int USER_NAME_UPDATE = 2000;

	private static final String USER_NAME_UPDATE_URL = app.HOST_URL
			+ "rmobile/function_update_userprofile_name.php";

	String Type;
	String getParam;
	HashMap<String, String> params;

	TextView text;
	EditText edit;

	DownLoadData upload;

	boolean isButtonClick = false;
	boolean isExit = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_userprofile_edit);

		setLayout();

		if (getIntent().getExtras() != null) {

			Type = getIntent().getExtras().getString("type");
			getParam = getIntent().getExtras().getString("param");
			edit.setText(getParam);

		} else {
			finish();
		}
		
	}

	private void setLayout() {
		text = (TextView) findViewById(R.id.a_userprofile_textview);
		edit = (EditText) findViewById(R.id.a_userprofile_edittext);
	}

	public void returnMessage(View v) {
		isButtonClick = true;
		isExit = true;
		getParam = edit.getText().toString();

		Intent intent = new Intent();
		intent.putExtra("resultValue", getParam);
		setResult(RESULT_OK, intent);

		params = new HashMap<String, String>();
		params.put("UserId", app.UserId);
		params.put("UserName", getParam);
		upload = new DownLoadData(USER_NAME_UPDATE_URL, params, 1);
		upload.setOnLoadComplete(new OnLoadComplete() {
			
			@Override
			public void onLoadComplete(String result, int type) {
				if (result.equals("0")) {
					app.UserName = getParam;
					finish();					
				}
			}
		});
		
		upload.setOnloadError(new OnLoadError() {
			
			@Override
			public void onLoadError(VolleyError err, int type) {
				err.printStackTrace();
				Log.v("NetworkError", upload.getUrl());
				Log.v("params", upload.getParam().get("UserId") + upload.getParam().get("UserName"));
			}
		});
		upload.commit(app.mQueue);
	}

}
